import pandas as pd
import numpy as np
import numpy.random as nr
from sklearn.cluster import KMeans, AgglomerativeClustering
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns #这个包要导对，我一开始没导对，一直报错


#读取数据
auto_prices = pd.read_excel('../预处理/预处理之后的数据.xlsx')

#统计不同气缸的数量对应的车辆数
# print(auto_prices['num-of-cylinders'].value_counts())

#汇总
# cylinders_categories = {'three':'three_fore','four':'three_fore',
#                         'five':'five_six','six':'five_six',
#                         'eight':'eight_twelve','twelve':'eight_twelve'}
#
# auto_prices['num-of-cylinders']=[cylinders_categories[x] for x in auto_prices['num-of-cylinders']]
# print(auto_prices['num-of-cylinders'].value_counts())

#箱型图
# def plot_box(auto_price,col,col_y='price'):
#     sns.set_style('whitegrid')
#     sns.boxplot(col,col_y,data=auto_price)
#     plt.xlabel(col)
#     plt.ylabel(col_y)
#     plt.show()
#
# plot_box(auto_prices,'num-of-cylinders')
# plot_box(auto_prices,'body-style')

#直方图
# def hist_plot(vals,lab):
#     sns.displot(vals)
#     plt.title('Histogram of '+ lab)
#     plt.xlabel('Value')
#     plt.ylabel('Density')
#     plt.show()
# hist_plot(auto_prices['price'],'price')
# auto_prices['log_price']=np.log(auto_prices['price'])  #转化为log
# hist_plot(auto_prices['price'],'price')

#可视化不同数据之间的关系
def plot_scatter_shape(auto_prices,cols, shape_col='fuel-type' ,col_y='price',alpha=0.2):
    shapes=['+','o','s','x','^']
    unique_cats=auto_prices[shape_col].unique()
    for col in cols:
        sns.set_style('whitegrid')
        for i,cat in enumerate(unique_cats):
            temp=auto_prices[auto_prices[shape_col]==cat]
            sns.regplot(col,col_y,data=temp,marker=shapes[i],label=cat,
                        scatter_kws={'alpha':alpha},fit_reg=False,color='blue')
        plt.title('Scatter plot of'+col_y+' vs. '+col)
        plt.xlabel(col)
        plt.ylabel(col_y)
        plt.legend
        plt.show()

num_cols=['curb-weight','engine-size','horsepower','city-mpg']
plot_scatter_shape(auto_prices,num_cols)


